package com.chenshu.formatter;

import com.chenshu.domain.Point;
import org.springframework.format.Formatter;
import org.springframework.util.StringUtils;

import java.text.ParseException;
import java.util.Locale;

/**
 * Created with IntelliJ IDEA
 * Description
 * User：chenshu
 * Date：2024-04-27
 * Time：20:15
 */
public class StringToPointFormatter implements Formatter<Point> {
    @Override
    public Point parse(String s, Locale locale) throws ParseException {
        if (StringUtils.hasLength(s)) {
            String[] arr = s.split(",");
            if (arr.length==2) {
                Point p = new Point();
                p.setX(Integer.parseInt(arr[0]));
                p.setY(Integer.parseInt(arr[1]));
                return p;
            }
        }
        return null;
    }

    @Override
    public String print(Point point, Locale locale) {
        if (point!=null) {
            return "(" + point.getX() + "," + point.getY() + ")";
         }
        return "";
    }
}
